package com.joplus.housekeeper.utils;

import android.app.Activity;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by ccm on 2016/10/8 10:17
 */

public class BackPageUtils {
    private Map<String, SoftReference<Activity>> back = new HashMap<>();

    private static BackPageUtils backPageUtils;

    private BackPageUtils() {
    }

    public static BackPageUtils getInstance() {

        if (backPageUtils == null) {
            synchronized (BackPageUtils.class) {
                backPageUtils = new BackPageUtils();
            }
        }
        return backPageUtils;

    }

    public final void addList(Activity activity) {
        if (activity != null) {
            back.put(activity.toString(), new SoftReference<Activity>(activity));
        }
    }

    public final void delList(Activity activity) {

        back.remove(activity.toString());
    }

    public final void clearList() {
        Iterator<Map.Entry<String, SoftReference<Activity>>> iterator = back.entrySet().iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next().getValue().get();
            if (activity != null) {
                if (!activity.isFinishing()) {
                    activity.finish();
                }
            }
        }
        back.clear();
    }


}
